﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class FeederRemarkRepo
    {
        public const string m_msg_head = "FeederRemark";

        public IEnumerable<FeederRemarkModels> GetFeederRemark()
        {
            List<FeederRemarkModels> FeederRemarks = new List<FeederRemarkModels>();
            string SQLCommand = @"select TBFeederRemark.*,TBTitleFeeDer.TitleFeeDerName from TBFeederRemark LEFT JOIN TBTitleFeeDer 
                                           ON TBFeederRemark.TitleFeeDerId = TBTitleFeeDer.TitleFeeDerId
                                           WHERE TBFeederRemark.Status <> 'Delete' ORDER BY TBFeederRemark.FeederRemarkTh";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        FeederRemarkModels FeederRemark = new FeederRemarkModels();
                        FeederRemark.Id = (int)reader["Id"];
                        FeederRemark.TitleFeeDerId = (int)reader["TitleFeeDerId"];
                        FeederRemark.TitleFeeDerName = (string)reader["TitleFeeDerName"];
                        FeederRemark.FeederRemarkTh = (string)reader["FeederRemarkTh"];
                        FeederRemark.FeederRemarkEng = (string)reader["FeederRemarkEng"];
                        FeederRemark.Status = (string)reader["Status"];
                        FeederRemark.CreateBy = (string)reader["CreateBy"];
                        FeederRemark.CreateOn = (DateTime)reader["CreateOn"];
                        FeederRemark.UpdateBy = (string)reader["UpdateBy"];
                        FeederRemark.UpdateOn = (DateTime)reader["UpdateOn"];

                        FeederRemarks.Add(FeederRemark);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return FeederRemarks.ToList();
        }

        public FeederRemarkModels GetFeederRemarkById(int id)
        {
            FeederRemarkModels FeederRemark = new FeederRemarkModels();
            string SQLCommand = @"select TBFeederRemark.*,TBTitleFeeDer.TitleFeeDerName from TBFeederRemark LEFT JOIN TBTitleFeeDer 
                                           ON TBFeederRemark.TitleFeeDerId = TBTitleFeeDer.TitleFeeDerId where TBFeederRemark.Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@Id", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        FeederRemark.Id = (int)reader["Id"];
                        FeederRemark.TitleFeeDerId = (int)reader["TitleFeeDerId"];
                        FeederRemark.TitleFeeDerName = (string)reader["TitleFeeDerName"];
                        FeederRemark.FeederRemarkTh = (string)reader["FeederRemarkTh"];
                        FeederRemark.FeederRemarkEng = (string)reader["FeederRemarkEng"];
                        FeederRemark.Status = (string)reader["Status"];
                        FeederRemark.CreateBy = (string)reader["CreateBy"];
                        FeederRemark.CreateOn = (DateTime)reader["CreateOn"];
                        FeederRemark.UpdateBy = (string)reader["UpdateBy"];
                        FeederRemark.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return FeederRemark;
        }

        public bool Create(FeederRemarkModels FeederRemark)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBFeederRemark
                                    values (@TitleFeeDerId,@FeederRemarkTh,@FeederRemarkEng,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleFeeDerId", FeederRemark.TitleFeeDerId);
                    db.AddParameter("@FeederRemarkTh", FeederRemark.FeederRemarkTh);
                    db.AddParameter("@FeederRemarkEng", FeederRemark.FeederRemarkEng);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID",FeederRemark.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(FeederRemarkModels FeederRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBFeederRemark 
                                    Set TitleFeeDerId=@TitleFeeDerId,
                                        FeederRemarkTh=@FeederRemarkTh,
                                        FeederRemarkEng=@FeederRemarkEng,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", FeederRemark.Id);
                    db.AddParameter("@TitleFeeDerId", FeederRemark.TitleFeeDerId);
                    db.AddParameter("@FeederRemarkTh", FeederRemark.FeederRemarkTh);
                    db.AddParameter("@FeederRemarkEng", FeederRemark.FeederRemarkEng);
                    db.AddParameter("@UserID", FeederRemark.UpdateBy );

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(FeederRemarkModels FeederRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBFeederRemark
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", FeederRemark.Id);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID", FeederRemark.UpdateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}